﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Photon.SocketServer;
using WorldOfSuperpowers_COMMON;
using WorldOfSuperpowers_COMMON.DataModel;

namespace WorldOfSuperpowers.Cache
{
    public class MatchRoomForTwo
    {
        public string[] PlayerAccount;
        public ushort[] BPSkillID = new ushort[2];
        private ushort ReadyPlayerCount = 0;

        private ushort currentBPStep;
        private ushort BanCount = 3;                  //前三个是BanSkill
        private ushort PickCount = 7;                  //第四个到第七个是PickSkill

        private ushort HasBPPlayerCount = 0;

        /// <summary>
        /// 生成新的游戏房间
        /// </summary>
        /// <param name="PlayerAccount"></param>
        public MatchRoomForTwo(string[] PlayerAccount)
        {
            //添加本房到1v1房间列表中
            MatchCenter.GetInstance().TwoPlayerRoomList.Add(this);
            int RoomIndex =  MatchCenter.GetInstance().TwoPlayerRoomList.IndexOf(this);
            this.PlayerAccount = PlayerAccount;
            for (int i = 0; i < PlayerAccount.Length; i++)
            {
                GetToMatchModel model = new GetToMatchModel();
                model.userPos = (ushort) i;
                model.oppositeName = UserCache.UserDataDicCache[PlayerAccount[i]].UserName;
                model.oppositeNameLevel = UserCache.UserDataDicCache[PlayerAccount[i]].UserLevel;
                model.IconIndex = UserCache.UserDataDicCache[PlayerAccount[i]].IconIndex;
                model.RoomIndex = (ushort)RoomIndex;
                object content_V = ProtoTranslater.GetInstance().GetBytesFromType(model);
                Dictionary<byte, object> content = new Dictionary<byte, object>();
                //将剩余玩家信息添加到消息内容中
                content.Add((byte)EventCode.GetToMatch, content_V);
                content.Add((byte)EventCode.MatchRoom, EventCode.GetToMatch);
                EventData data = new EventData();
                data.Code = (byte)EventCode.MatchRoom;
                //添加消息内容
                data.Parameters = content;
                //给另一家发送这一家的消息
                UserCache.AllConnectedPeerDic[PlayerAccount[1-i]].SendEvent(data, new SendParameters());
            }
            //todo 开启监听对局玩家进入匹配界面
            #region  多人房间消息(备用)
            ////给玩家发送本局其他玩家信息
            //Dictionary<string,GetToMatchModel> modelDic = new Dictionary<string, GetToMatchModel>();
            //for (int i = 0; i < PlayerAccount.Length; i++)
            //{
            //    GetToMatchModel model = new GetToMatchModel();
            //    //给每个玩家独立的id
            //    model.userPos = (ushort)i;
            //    model.oppositeName = UserCache.UserDataDicCache[PlayerAccount[i]].UserName;
            //    model.oppositeNameLevel = UserCache.UserDataDicCache[PlayerAccount[i]].UserLevel;
            //    modelDic.Add(PlayerAccount[i],model);
            //}
            //for (int i = 0; i < PlayerAccount.Length; i++)
            //{
            //    //获取当前玩家信息
            //    GetToMatchModel myModel = modelDic[PlayerAccount[i]];
            //    //删除这个包内当前玩家的信息，不发给他
            //    modelDic.Remove(PlayerAccount[i]);
            //    Dictionary<byte,object> content = new Dictionary<byte, object>();
            //    //将剩余玩家信息添加到消息内容中
            //    content.Add((byte)EventCode.GetToMatch,modelDic.Values.ToList());
            //    content.Add((byte)EventCode.MatchRoom,EventCode.GetToMatch);
            //    EventData data = new EventData();
            //    data.Code = (byte)EventCode.MatchRoom;
            //    //添加消息内容
            //    data.Parameters = content;
            //    //发送消息
            //    UserCache.AllConnectedPeerDic[PlayerAccount[i]].SendEvent(data, new SendParameters());
            //    //将本玩家信息放回字典中，方便下个玩家消息使用
            //    modelDic.Add(PlayerAccount[i],myModel);
            //}
            #endregion
        }

        /// <summary>
        /// 收到玩家已进入匹配界面消息
        /// </summary>
        public void GetPlayerReadyMessage(ushort UserPos)
        {
            ReadyPlayerCount++;
            if (ReadyPlayerCount == 2)
            {
                //如果双方都准备好了，通知双方第一轮BP选择
                EventData data = new EventData();
                data.Code = (byte)EventCode.MatchRoom;
                Dictionary<byte, object> content = new Dictionary<byte, object>();
                content.Add((byte)EventCode.MatchRoom,EventCode.ChooseSkillToBan);
                //给双方发送BanSkill消息
                data.Parameters = content;
                for (int i = 0; i < PlayerAccount.Length; i++)
                {
                    UserCache.AllConnectedPeerDic[PlayerAccount[i]].SendEvent(data, new SendParameters());
                }
                currentBPStep = 1;
                ReadyPlayerCount = 0;
            }
        }

        /// <summary>
        /// 收到BP消息，在此处处理
        /// </summary>
        public void GetBPMessage(SkillModel Smodel)
        {
            HasBPPlayerCount++;
            BPSkillID[Smodel.userPos] = Smodel.skillID;
            if (HasBPPlayerCount==2)
            {
                //两人都点击了，判断下一次时Ban还是Pick
                currentBPStep++;
                if (currentBPStep > PickCount)
                {
                    //BP全部结束,进入倒计时并发送最后一次BP结果，准备进入对战场景
                    //还是发送上一次选择技能的结果
                    BPInfos model = new BPInfos();
                    model.SkillID = BPSkillID;
                    model.SkillisBan = false;
                    object BPData = ProtoTranslater.GetInstance().GetBytesFromType(model);
                    SendBPEventMessage(EventCode.CountDown, BPData);
                    Timer timer = new Timer(10000);
                    timer.Elapsed += TimerOnElapsed;
                    timer.AutoReset = false;    //只发送一次
                    timer.Enabled = true;
                    MasterServer.log.Debug("倒计时结束，游戏开始");
                }
                else if (currentBPStep > BanCount)
                {
                    //继续发送Pick指令
                    BPInfos model = new BPInfos();
                    model.SkillID = BPSkillID;
                    //当前步骤如果比禁用次数大1，本次发送的依旧是禁用结果，还是发送禁用信息
                    model.SkillisBan = (currentBPStep - BanCount == 1);
                    object BPData = ProtoTranslater.GetInstance().GetBytesFromType(model);
                    SendBPEventMessage(EventCode.ChooseSkillToPick, BPData);
                }
                else
                {
                    //继续发送Ban指令
                    BPInfos model = new BPInfos();
                    model.SkillID = BPSkillID;
                    model.SkillisBan = true;
                    object BPData = ProtoTranslater.GetInstance().GetBytesFromType(model);
                    SendBPEventMessage(EventCode.ChooseSkillToBan, BPData);
                }
                HasBPPlayerCount = 0;
                BPSkillID = new ushort[2];
            }
        }

        private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
        {
            MasterServer.log.Debug("倒计时结束，游戏开始");
            SendBPEventMessage(EventCode.BeginGame, null);
        }

        /// <summary>
        /// 发送BP指令(包含下一次选择结果)
        /// </summary>
        /// <param name="code"></param>
        /// <param name="model"></param>
        public void SendBPEventMessage(EventCode code,object model)
        {
            EventData data = new EventData();
            data.Code = (byte)EventCode.MatchRoom;
            Dictionary<byte, object> content = new Dictionary<byte, object>();
            content.Add((byte)EventCode.MatchRoom, code);
            if(model != null)
                content.Add((byte)code,model);
            //给双方发送BanSkill消息
            data.Parameters = content;
            for (int i = 0; i < PlayerAccount.Length; i++)
            {
                UserCache.AllConnectedPeerDic[PlayerAccount[i]].SendEvent(data, new SendParameters());
            }
        }

        /// <summary>
        /// 房间使用完毕，移除
        /// </summary>
        public void CleanRoom()
        {
            PlayerAccount = null;
            MatchCenter.GetInstance().TwoPlayerRoomList.Remove(this);
            ReadyPlayerCount = 0;
        }

    }
}
